extends Node

signal emotion_changed(emotion: String)

enum PetState { IDLE, SHOCK, SLEEP }
var state: PetState = PetState.IDLE

@export var idle_duration := 10.0
@export var sleep_duration := 30.0

var time_since_last_interaction := 0.0

func _process(delta):
	time_since_last_interaction += delta

	match state:
		PetState.IDLE:
			if time_since_last_interaction > sleep_duration:
				_change_state(PetState.SLEEP)
		PetState.SLEEP:
			pass
		PetState.SHOCK:
			pass

func interact():
	if state == PetState.SLEEP:
		_change_state(PetState.SHOCK)
	elif state == PetState.IDLE:
		time_since_last_interaction = 0.0

func ask_triggered():  # 主动提问
	_change_state(PetState.SHOCK)

func _change_state(new_state: PetState):
	state = new_state
	time_since_last_interaction = 0.0

	match new_state:
		PetState.IDLE:
			emit_signal("emotion_changed", "idle")
		PetState.SLEEP:
			emit_signal("emotion_changed", "sleep")
		PetState.SHOCK:
			emit_signal("emotion_changed", "shock")
			await get_tree().create_timer(1.0).timeout
			_change_state(PetState.IDLE)
